package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DtsCategoryServiceImpl implements DtsCategoryService {

    @Autowired
    DtsCategoryMapper categoryMapper;

    @Override
    public List<Integer> findCategoryById(Integer cateId) {

        DtsCategory category = categoryMapper.selectById(cateId);

        Integer pid = category.getPid();

        List<Integer> ids = new ArrayList<>();
        ids.add(pid);
        ids.add(cateId);

        return ids;
    }

    @Override
    public List<CatVo> findCategoryList() {

        //首先查询一级分类列表
        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCategory::getDeleted, false);
        wrapper.eq(DtsCategory::getPid, 0);

        List<DtsCategory> categoryList = categoryMapper.selectList(wrapper);

        if(categoryList != null){
            List<CatVo> cateVoList = categoryList.stream().map(category -> {
                //一个分类对象  对应一个catvo对象
                CatVo catVo = new CatVo();
                catVo.setValue(category.getId());
                catVo.setLabel(category.getName());

                //查询二级分类列表
                LambdaQueryWrapper<DtsCategory> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DtsCategory::getDeleted, false);
                wrapper1.eq(DtsCategory::getPid, category.getId());
                List<DtsCategory> categories = categoryMapper.selectList(wrapper1);
                if(categories != null){
                    List<CatVo> cateVoList1 = categories.stream().map(category1 -> {
                        //一个分类对象  对应一个catvo对象
                        CatVo catVo1 = new CatVo();
                        catVo1.setValue(category.getId());
                        catVo1.setLabel(category.getName());

                        return catVo1;
                    }).collect(Collectors.toList());

                    //设置二级分类列表
                    catVo.setChildren(cateVoList1);
                }

                return catVo;
            }).collect(Collectors.toList());

            return cateVoList;
        }

        return null;
    }

    @Override
    public List<CatVo> findCategoryL1() {

        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCategory::getDeleted, false);
        wrapper.eq(DtsCategory::getPid, 0);

        List<DtsCategory> categoryList = categoryMapper.selectList(wrapper);

        if(categoryList != null){
            List<CatVo> cateVoList = categoryList.stream().map(category -> {
                //一个分类对象  对应一个catvo对象
                CatVo catVo = new CatVo();
                catVo.setValue(category.getId());
                catVo.setLabel(category.getName());

                return catVo;
            }).collect(Collectors.toList());

            return cateVoList;
        }
        return null;
    }

    @Override
    public void update(DtsCategory category) {

        category.setUpdateTime(new Date());
        categoryMapper.updateById(category);

    }

    @Override
    public void insert(DtsCategory category) {

        category.setAddTime(new Date());
        category.setUpdateTime(new Date());

        categoryMapper.insert(category);

    }

    @Override
    public void delete(DtsCategory category) {

        categoryMapper.deleteById(category.getId());

    }

    @Override
    public boolean isExists(Integer categoryId) {

        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCategory::getDeleted, false);
        wrapper.eq(DtsCategory::getId, categoryId);

        int count = categoryMapper.selectCount(wrapper);
        return count != 0;
//        if(count == 0){
//            return false;
//        }
//        return true;
    }

    @Override
    public IPage<DtsCategory> queryPage(Integer page, Integer limit, Integer id, String name, String sort, String order) {

        IPage<DtsCategory> iPage = new Page<>(page, limit);

        QueryWrapper<DtsCategory> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(id)){
            wrapper.like("id", id);
        }

        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }

        if(order.equalsIgnoreCase("desc")){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        iPage = categoryMapper.selectPage(iPage, wrapper);

        return iPage;
    }

}
